package com.www.applyword.ui.home;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.www.applyword.R;
import com.www.applyword.base.BaseActivity;
import com.www.applyword.utils.PackageManagerUtil;
import com.www.applyword.utils.SPUtil;
import com.www.applyword.utils.Utils;

import java.net.URISyntaxException;

/**
 * author : huang
 * date   : 2019/12/12 0012 18:19
 * version: 1.0
 */
public class LocationInfoActivity extends BaseActivity {

    private MapView mapView;
    private AMap aMap;//地图控制器对象
    private double order_workLat;
    private double order_workLng;
    private String latitude;
    private String longitude,address,address2;


    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.location_info_activity);
        setTitle("显示位置");
        setRringTitle("路线");

        latitude = SPUtil.getString("latitude");
        longitude = SPUtil.getString("longitude");
        address = SPUtil.getString("address");
        tv_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    // 高德地图 先维度——后经度
                    Intent intent = Intent.getIntent("androidamap://route?sourceApplication=softname&slat="
                                    + latitude   //latitude  起点纬度
                                    + "&slon="
                                    + longitude    //longitude  //起点经度
                                    + "&sname="
                                    + address
                                    + "&dlat="
                                    + order_workLat
                                    + "&dlon="
                                    + order_workLng
                                    + "&dname="
                                    + address2 + "&dev=0&m=0&t=1");

                    //启动路径规划页面   //传入经度和纬度 和 地名
                    String uri = "amapuri://route/plan/?dlat="+ order_workLat+"&dlon="+order_workLng+"&dname="+ address2+"&dev=1&t=1";
                    if (PackageManagerUtil.haveGaodeMap()) {
                        Intent intent2 = new Intent("android.intent.action.VIEW", android.net.Uri.parse(uri));
                        intent2.setPackage("com.autonavi.minimap");
                        startActivity(intent2);
                        Utils.showToastSafe("高德地图正在启动");
                    } else {
                        Utils.showToastSafe("高德地图没有安装");
                        Intent i = new Intent();
                        i.setData(Uri.parse("http://daohang.amap.com/index.php?id=201&CustomID=C021100013023"));
                        i.setAction(Intent.ACTION_VIEW);
                        startActivity(i); //启动浏览器
                    }
                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mapView = (MapView) findViewById(R.id.location_mapview);
        mapView.onCreate(savedInstanceState);
        //初始化地图控制器对象
        if (mapView != null) {
            aMap = mapView.getMap();
            // 自定义系统定位小蓝点
            MyLocationStyle myLocationStyle = new MyLocationStyle();
            myLocationStyle.strokeColor(0x7F0070D9);// 设置圆形的边框颜色
            myLocationStyle.radiusFillColor(0x130070D9);// 设置圆形的填充颜色
            myLocationStyle.strokeWidth(1.0f);// 设置圆形的边框粗细
            //  myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE);//定位一次，且将视角移动到地图中心点
            // aMap.setMyLocationStyle(myLocationStyle);
            //  //设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false
            // aMap.setMyLocationEnabled(true);
            // 设置地图的缩放级别取值范围[3-20]值越大，显示的越详细
            aMap.getUiSettings().setZoomControlsEnabled(false);
            // aMap.moveCamera(CameraUpdateFactory.zoomTo(17f));
        }
        order_workLat = getIntent().getDoubleExtra("order_workLat", 0);
        order_workLng = getIntent().getDoubleExtra("order_workLng", 0);
        address2 = getIntent().getStringExtra("address");
        aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(order_workLat, order_workLng), 17));
        //绘制marker
        aMap.addMarker(new MarkerOptions()
                .position(new LatLng(order_workLat, order_workLng))
                .draggable(true));
    }

    @Override
    protected void loadData() {

    }
}
